<?

require_once("utilities.php");
require_once("mysql.php");

SESSION();

function SESSION() {

    @session_start();

}

function SESSION_GET($key) {

    return @$_SESSION[$key];

}

function SESSION_SET($key, $value) {

    @$_SESSION[$key] = $value;

}


function find_or_create_session($count = 1) {

    $session = trim(session_id());
    $ip = IP2();


    $q = "SELECT * FROM session WHERE session='%s' AND ip='%s'";
    $q = sprintf($q, $session, $ip);
    $r = MYSQL_Q($q);
    $c = mysql_num_rows($r);

    if ($c == 0) {
     
        MYSQL_Q("INSERT INTO session (session, ip, time, count) VALUES ('".$session."' , '".$ip."', UNIX_TIMESTAMP(NOW()), 1)");
        return mysql_insert_id();
    
    } else {
    
        $entry = mysql_fetch_assoc($r);

        if ($count) {
            MYSQL_Q("UPDATE session SET count=".($entry['count']+1).",  time=UNIX_TIMESTAMP(NOW()) WHERE session='".$session."' AND ip='".$ip."'");
        }
        
        return $entry['id'];
    }
}


function session_save_vars($id, $vars) {

   
    MYSQL_Q("UPDATE session SET variables='".ESC($vars)."' WHERE id=".$id);

    
}



if (strpos($_SERVER['PHP_SELF'], "/session.php")) {

    $id = find_or_create_session();

    $r = MYSQL_Q("SELECT * FROM session WHERE id=".$id);
    $row = mysql_fetch_assoc($r);

    echo "\"".$row['variables']."\"";
 

    if (@isset($_GET['save'])) {  

        session_save_vars($id, ESC($_GET['variables']));

    }

}




?>